package com.lzb.yabbs.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.service.voice.VoiceInteractionService;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.lzb.yabbs.AppConfig;
import com.lzb.yabbs.R;
import com.lzb.yabbs.adapter.BoardAdapter;
import com.lzb.yabbs.dao.Board;
import com.lzb.yabbs.interfaces.GetBoardCallback;
import com.lzb.yabbs.interfaces.OnFragmentInteractionListener;
import com.lzb.yabbs.interfaces.OnItemClickListener;
import com.lzb.yabbs.net.GetBoardFromNet;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ModuleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ModuleFragment extends Fragment implements OnItemClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    private RecyclerView mRecyclerView;
    private List<Board> mBoard=new ArrayList<>();
    private final int GET_BOARD_SUCESS=0x123;
    private final int GET_BOARD_FAIL=0x124;

    private BoardAdapter mBoardAdapter;
    private ProgressBar mProgressBar;

    public Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==GET_BOARD_SUCESS){
                mBoardAdapter.addBoards(mBoard);
                mProgressBar.setVisibility(View.GONE);
            }else if(msg.what==GET_BOARD_FAIL){
                mProgressBar.setVisibility(View.GONE);
            }
        }
    };

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ModuleFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ModuleFragment newInstance(String param1, String param2) {
        ModuleFragment fragment = new ModuleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public ModuleFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_module, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView= (RecyclerView) view.findViewById(R.id.board_rv);
        mProgressBar= (ProgressBar) view.findViewById(R.id.progressbar);

        RecyclerView.LayoutManager mLayoutManager=new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);

        mBoardAdapter=new BoardAdapter(mBoard,getActivity());
        mBoardAdapter.setmOnItemClickListener(this);
        mRecyclerView.setAdapter(mBoardAdapter);

        getBoards();
    }

    private void getBoards(){
        GetBoardFromNet.getBoards(AppConfig.URL_GET_BOARDS, new GetBoardCallback() {
            @Override
            public void success(List<Board> mBoards) {
                System.out.println("success mBoards:"+mBoards.size());
                mBoard=mBoards;
                mHandler.sendEmptyMessage(GET_BOARD_SUCESS);
            }

            @Override
            public void fail(int reason) {
                mHandler.sendEmptyMessage(GET_BOARD_FAIL);
            }
        });
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onItemClick(int position) {
        Intent intent=new Intent(getActivity(),CardListActivity.class);
        intent.putExtra("board",mBoard.get(position));
        startActivity(intent);
    }
}
